﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BabyToys.Models;
using PagedList;
using PagedList.Mvc;
using System.Web.Helpers;
using System.IO;
using System.Data;
namespace BabyToys.Controllers
{
    public class AdminThuongHieuController : Controller
    {
        //
        // GET: /AdminThuongHieu/
        DatabaseContext db = new DatabaseContext();
        public ActionResult Index(int? page)
        {
            var pageNumber = page ?? 1;
            var thuonghieus = db.ThuongHieus.ToList();
            ViewBag.onePageOfThuongHieu = thuonghieus.ToPagedList(pageNumber, 10);
            return View();
        }

        public ActionResult ThemThuongHieu()
        {
            return View();
        }
    
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ThemThuongHieu(ThuongHieu thuonghieu)
        {
            if (ModelState.IsValid)
            {
                var image = WebImage.GetImageFromRequest("anhdaidien");
                if (image != null)
                {
                    string ImageName = Path.GetFileName(image.FileName);
                    ImageName = "main" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day +
                        DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + "-" + ImageName;
                    var pathToSave = Path.Combine(Server.MapPath("~/Content/Images/ThuongHieu"), ImageName);
                    image.Save(pathToSave);
                    thuonghieu.Logo = ImageName;
                }
                else
                {
                    thuonghieu.Logo="noimageyet.jpg";
                }
                db.ThuongHieus.Add(thuonghieu);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(thuonghieu);
        }

        public ActionResult SuaThuongHieu(int id)
        {
            var thuonghieu = db.ThuongHieus.SingleOrDefault(t => t.IdThuongHieu == id);
            return View(thuonghieu);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SuaThuongHieu(ThuongHieu thuonghieu)
        {
            if (ModelState.IsValid)
            {
                //var image = WebImage.GetImageFromRequest("anhdaidien");
                var image = WebImage.GetImageFromRequest("anhdaidien");
                if (image != null)
                {
                    string ImageName = Path.GetFileName(image.FileName);
                    ImageName = "main" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day +
                        DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + "-" + ImageName;
                    var pathToSave = Path.Combine(Server.MapPath("~/Content/Images/ThuongHieu"), ImageName);
                    image.Save(pathToSave);
                    thuonghieu.Logo = ImageName;
                }
                db.Entry(thuonghieu).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(thuonghieu);
        }

    }
}
